

#pragma once

#include "zAcString.h"
#include "zAcValue.h"

class UcValue;
class UcDbLinkedData;
class UcDbDataLink;
class UcDbDataLinkManager;
class UcDbImpDataLinkManager;
class UcDbDataAdapter;
class UcTableSystemInternals;


#define UCDB_DATA_ADAPTER_EXCEL         UCRX_T("AcExcel")

class UcDbDataLink : public UcDbObject
{
public:
    UCDB_DECLARE_MEMBERS(UcDbDataLink);

    UCDB_PORT UcDbDataLink(void);
    UCDB_PORT virtual ~UcDbDataLink(void);

    UCDB_PORT UcString name                 (void) const;
    UCDB_PORT Ucad::ErrorStatus setName     (const ZTCHAR* pszName);
    UCDB_PORT const ZTCHAR* description      (void) const;
    UCDB_PORT Ucad::ErrorStatus setDescription(const ZTCHAR* pszDescription);
    UCDB_PORT UcString getToolTip           (void) const;
    UCDB_PORT Ucad::ErrorStatus setToolTip  (const ZTCHAR* pszToolTip);
    UCDB_PORT const ZTCHAR* dataAdapterId    (void) const;
    UCDB_PORT Ucad::ErrorStatus setDataAdapterId(const ZTCHAR* pszAdapterId);
    UCDB_PORT const ZTCHAR* connectionString (void) const;
    UCDB_PORT Ucad::ErrorStatus setConnectionString(const ZTCHAR* pszConnectionString);
    UCDB_PORT UcDb::DataLinkOption option   (void) const;
    UCDB_PORT Ucad::ErrorStatus setOption   (UcDb::DataLinkOption nOption);
    UCDB_PORT int   updateOption            (void) const;
    UCDB_PORT Ucad::ErrorStatus setUpdateOption(int nOption);
    UCDB_PORT Ucad::ErrorStatus update      (UcDb::UpdateDirection nDir, 
                                             UcDb::UpdateOption nOption);
    UCDB_PORT Ucad::ErrorStatus getUpdateStatus(UcDb::UpdateDirection* pDir, 
                                             SYSTEMTIME* pTime, 
                                             UcString* pErrMessage) const;
    UCDB_PORT Ucad::ErrorStatus setUpdateStatus(const ZTCHAR* pszErrMessage);
    UCDB_PORT bool  isValid                 (void) const;
    UCDB_PORT int   getTargets              (UcDbObjectIdArray& targetIds) const;
    UCDB_PORT Ucad::ErrorStatus getSourceFiles(UcDb::DataLinkGetSourceContext nContext, 
                                             UcStringArray& files) const;
    UCDB_PORT Ucad::ErrorStatus repathSourceFiles(LPCTSTR pszBasePath, 
                                             UcDb::PathOption nOption);
    UCDB_PORT Ucad::ErrorStatus getCustomData(const ZTCHAR* pszKey, 
                                             UcValue* pData) const;
    UCDB_PORT Ucad::ErrorStatus setCustomData(const ZTCHAR* pszKey, 
                                             const UcValue* pData);

    UCDB_PORT virtual Ucad::ErrorStatus dwgInFields(UcDbDwgFiler* pFiler);
    UCDB_PORT virtual Ucad::ErrorStatus dwgOutFields(UcDbDwgFiler* pFiler) const;
    UCDB_PORT virtual Ucad::ErrorStatus dxfInFields(UcDbDxfFiler* pFiler);
    UCDB_PORT virtual Ucad::ErrorStatus dxfOutFields(UcDbDxfFiler* pFiler) const;
};

typedef UcArray<UcDbDataLink*> UcDbDataLinkArray;

class UcDbDataLinkManager : public UcRxObject
{
public:
    UCRX_DECLARE_MEMBERS(UcDbDataLinkManager);

    UCDB_PORT virtual ~UcDbDataLinkManager(void);

    UCDB_PORT Ucad::ErrorStatus createDataLink(const ZTCHAR* pszAdapterId, 
                                             const ZTCHAR* pszName, 
                                             const ZTCHAR* pszDescription, 
                                             const ZTCHAR* pszConnectionString, 
                                             UcDbObjectId& idDataLink);
    UCDB_PORT Ucad::ErrorStatus getDataLink (const ZTCHAR* pszName, 
                                             UcDbObjectId& idDataLink) const;
    UCDB_PORT Ucad::ErrorStatus getDataLink (const ZTCHAR* pszName, 
                                             UcDbDataLink*& pDataLink, 
                                             UcDb::OpenMode mode) const;
    UCDB_PORT int   dataLinkCount           (void) const;
    UCDB_PORT int   getDataLink             (UcDbObjectIdArray& dataLinks) const;
    UCDB_PORT Ucad::ErrorStatus addDataLink (UcDbDataLink* pDataLink);
    UCDB_PORT Ucad::ErrorStatus addDataLink (UcDbDataLink* pDataLink, 
                                             UcDbObjectId& idDataLink);
    Ucad::ErrorStatus removeDataLink        (const ZTCHAR* pszKey, 
                                             UcDbObjectId& idDataLink);
    UCDB_PORT Ucad::ErrorStatus removeDataLink(const UcDbObjectId& idDataLink);

    UCDB_PORT Ucad::ErrorStatus update      (const UcDbObjectIdArray& dataIds, 
                                             UcDb::UpdateDirection nDir,
                                             UcDb::UpdateOption nOption);
    UCDB_PORT Ucad::ErrorStatus update      (UcDb::UpdateDirection nDir, 
                                             UcDb::UpdateOption nOption);

protected:
    UCDB_PORT UcDbDataLinkManager(void);

private:
    void        *   mpImpObj;
    friend class UcTableSystemInternals;
    friend class UcDbImpDataLinkManager;
};
